package com.jlnk.okhttpdemo.views;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.jlnk.okhttpdemo.R;
import com.jlnk.okhttpdemo.adapter.HotSearchAdapter;
import com.jlnk.okhttpdemo.entity.HotSearchItem;
import com.jlnk.okhttpdemo.entity.ResultWrapper;
import com.jlnk.okhttpdemo.utils.HttpUtil;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private HotSearchAdapter adapter;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food);

        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new HotSearchAdapter(this);
        recyclerView.setAdapter(adapter);

        editText = findViewById(R.id.edit);

        Button getButton = findViewById(R.id.button_get);
        getButton.setOnClickListener(v -> fetchHotSearchData());

        Button postButton = findViewById(R.id.button_post);
        postButton.setOnClickListener(v -> {
            String text = editText.getText().toString();
            if (!text.isEmpty()) {
                postTranslation(text);
            }
        });
    }

    private void fetchHotSearchData() {
        String url = "https://api.oioweb.cn/api/common/fetchHotSearchBoard?type=baidu";
        HttpUtil.get(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                Log.e("OkHttp", "Request failed", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final String jsonData = response.body().string();
                    parseAndDisplayData(jsonData);
                } else {
                    Log.e("OkHttp", "Unexpected code " + response);
                }
            }
        });
    }

    private void parseAndDisplayData(String jsonData) {
        try {
            Gson gson = new Gson();
            ResultWrapper resultWrapper = gson.fromJson(jsonData, ResultWrapper.class);
            if (resultWrapper != null && resultWrapper.result != null) {
                List<HotSearchItem> items = resultWrapper.result;

                runOnUiThread(() -> {
                    adapter.setItems(items);
                    adapter.notifyDataSetChanged();
                });
            } else {
                Log.e("Gson", "JSON data does not contain 'result' field or field is null.");
            }
        } catch (JsonSyntaxException e) {
            Log.e("Gson", "Error parsing JSON", e);
        }
    }

    private void postTranslation(String sourceText) {
        if (sourceText == null || sourceText.trim().isEmpty()) {
            Log.e("postTranslation", "Source text must not be empty.");
            return;
        }

        String url = "https://api.oioweb.cn/api/txt/QQFanyi";
        String params = "sourceText=" + sourceText;
        HttpUtil.post(url, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                Log.e("OkHttp", "Request failed", e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    final String jsonData = response.body().string();
                    Log.d("Translation", jsonData);
                    runOnUiThread(() -> {
                    });
                } else {
                    Log.e("OkHttp", "Unexpected code " + response);
                }
            }
        });
    }
}